<template>
  <div class="count-box">
    <button class="minus" @click="minus">-</button>
    <input :value="value" @change="changeCount" type="text" class="inp" >
    <button class="add" @click="add">+</button>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    add () {
      this.$emit('input', this.value + 1)
    },
    minus () {
      if (this.value > 1) {
        this.$emit('input', this.value - 1)
      }
    },
    changeCount (e) {
      const num = +e.target.value
      if (isNaN(num)) {
        e.target.value = this.value
        return
      }
      this.$emit('input', e.target.value)
    }
  }
}
</script>

<style lang="less" scoped>
.count-box {
    width:110px;
    display:flex;
    .add, .minus {
        width: 30px;
        height: 30px;
        outline: none;
        border:none;
        background-color: #efefef;
    }
    .inp {
        width: 40px;
        height: 30px;
        text-align: center;
        outline: none;
        border: none;
        margin: 0 5px;
        background-color: #efefef;
    }
}
</style>
